package com.tusuo.stds.ui.module;

import android.content.Context;

import com.hannesdorfmann.mosby.mvp.MvpBasePresenter;
import com.tusuo.stds.MyApp;
import com.tusuo.stds.R;
import com.tusuo.stds.di.DaggerScope;
import com.tusuo.stds.di.Injector;
import com.tusuo.stds.rest.ApiHelper;
import com.tusuo.stds.rest.LevelList;
import com.tusuo.stds.util.Utils;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import autodagger.AutoComponent;
import autodagger.AutoInjector;
import rx.Subscriber;
import timber.log.Timber;

/**
 * @author J.Huang
 * @version 1.0
 *
 */
@AutoComponent(
        dependencies = MyApp.class
)
@AutoInjector
@DaggerScope(ModulePresenter.class)
class ModulePresenter extends MvpBasePresenter<ModuleView> {

    @Inject
    ApiHelper mApiHelper;

    private Context mContext;
    private String mErrMsgServer;

    private Subscriber<LevelList> mGetLevelListSubscriber = null;

    private int mModuleId;

    ModulePresenter(Context context, int moduleId) {
        mContext = context;
        mErrMsgServer = mContext.getResources().getString(R.string.err_msg_server);
        mModuleId = moduleId;

        Injector.initModulePresenterComponent().inject(this);
    }

    @Override
    public void attachView(ModuleView view) {
        super.attachView(view);
    }

    @Override
    public void detachView(boolean retainInstance) {
        if (mGetLevelListSubscriber != null) {
            mGetLevelListSubscriber.unsubscribe();
            mGetLevelListSubscriber = null;
        }
        super.detachView(retainInstance);
    }

    void loadData() {
        mGetLevelListSubscriber = new GetLevelListSubscriber();
        mApiHelper.getLevelList(mGetLevelListSubscriber, mModuleId);
    }

    private class GetLevelListSubscriber extends Subscriber<LevelList> {
        @Override
        public void onCompleted() {}

        @Override
        public void onError(Throwable e) {
            Timber.d(e.toString());
            Utils.UI.showToastShort(mErrMsgServer);
        }

        @Override
        public void onNext(final LevelList list) {
            if (list.checkState(mContext) && getView() != null) {
                if (list.MainLevels.size() < 1 && list.BonusLevels.size()< 1) {
                    getView().showNoLevelsTips();
                } else {
                    List<LevelList.Level> row = new ArrayList<>(2);

                    List<List<LevelList.Level>> mainList = new ArrayList<>();
                    for (int i = 0; i < list.MainLevels.size(); i++) {
                        if (i % 2 == 0) {   // left one
                            row = new ArrayList<>(2);

                            row.add(list.MainLevels.get(i));

                            // 检查是否是最后一个数据
                            if (i + 1 == list.MainLevels.size()) {
                                mainList.add(row);
                            }

                        } else {    // right one
                            row.add(list.MainLevels.get(i));
                            mainList.add(row);
                        }
                    }

                    List<List<LevelList.Level>> bonusList = new ArrayList<>();
                    for (int i = 0; i < list.BonusLevels.size(); i++) {
                        if (i % 2 == 0) {   // left one
                            row = new ArrayList<>(2);

                            row.add(list.BonusLevels.get(i));
                            // 检查是否是最后一个数据
                            if (i + 1 == list.BonusLevels.size()) {
                                bonusList.add(row);
                            }
                        } else {    // right one
                            row.add(list.BonusLevels.get(i));
                            bonusList.add(row);
                        }
                    }

                    LevelListAdapter mainAdapter = new LevelListAdapter(mContext, mainList, true);

                    LevelListAdapter bonusAdapter;
                    if (bonusList.size() == 0) {
                        bonusAdapter = null;
                    } else {
                        bonusAdapter = new LevelListAdapter(mContext, bonusList, true);
                    }

                    getView().setListViewAdapter(mainAdapter, bonusAdapter);
                }
            }
        }
    }
}
